const express = require('express');
const os = require('os')
const path = require('path')
const session = require('express-session')
const MongoStore = require('connect-mongo')(session)
// eslint-disable-next-line no-undef
const config = require('config-lite')(__dirname)

const router = require('./routes/index')
const app = express()

// 设置静态引擎
// eslint-disable-next-line no-undef
app.use(express.static(path.join(__dirname, 'public')))

// session中间件
app.use(session({
  name: config.session.key,
  secret: config.session.secret,
  resave: true,
  saveUninitialized: false,
  cookie: {
    maxAge: config.session.maxAge
  },
  store: new MongoStore({
    url: config.mongodb
  })
}))

router(app)

app.listen(3000, () => {
  const ipv4 = os.networkInterfaces().en0.find(item => item.family === 'IPv4').address
  console.log(`服务器已启动：http://${ipv4}:3000`)
})
